--[[
--
--  registry_profileselector
--
--]]

local function GetLocale()
 if realLocale ~= "deDE" and realLocale ~= "enUS" then 
  return "enUS";
 else
  return realLocale;
 end
end

local CONFIRM_DELETE = {
 ["deDE"] = "Wollen sie das Profil $s wirklich l\195\182schen?",
 ["enUS"] = "Do you really wish to delete $s ?",
};
local NAME_PROFILE = {
 ["deDE"] = "Name des neuen Profiles",
 ["enUS"] = "Enter the name for the new profile",
};

local DELETE_PROFILE = {
 ["deDE"] = "L\195\182schen",
 ["enUS"] = "Delete",
};
local SWITCH_PROFILE = {
 ["deDE"] = "Laden",
 ["enUS"] = "Load",
};
local CREATE_PROFILE = {
 ["deDE"] = "Neu...",
 ["enUS"] = "Create",
};

function cr_ProfileSelector()
if getglobal("RegistryWndProfileSelector") == nil then
local ProfileSelector = CreateFrame("Frame","RegistryWndProfileSelector",RegistryWnd);

ProfileSelector:SetBackdrop({bgFile = "Interface/Tooltips/UI-Tooltip-Background", 
                                            edgeFile = "Interface/Tooltips/UI-Tooltip-Border", 
                                            tile = true, tileSize = 16, edgeSize = 16, 
                                            insets = { left = 4, right = 4, top = 4, bottom = 4 }});
ProfileSelector:SetBackdropColor(0,0,0,1);

local control = CreateFrame("Button", "Profile", ProfileSelector, "UIDropDownMenuTemplate");
  
  control:SetPoint("TOPLEFT",ProfileSelector,"TOPLEFT",-4,-11);
  control:SetParent(ProfileSelector);
  getglobal(control:GetName().."Button"):SetFrameLevel(5);
  UIDropDownMenu_SetWidth(128, control);
local DeleteBTN = CreateFrame("Button", "Delete", ProfileSelector, "UIPanelButtonTemplate")
local SwitchBTN = CreateFrame("Button", "Switch", ProfileSelector, "UIPanelButtonTemplate")
local CreateBTN = CreateFrame("Button", "Create", ProfileSelector, "UIPanelButtonTemplate")

DeleteBTN:SetText(DELETE_PROFILE[GetLocale()]);
SwitchBTN:SetText(SWITCH_PROFILE[GetLocale()]);
CreateBTN:SetText(CREATE_PROFILE[GetLocale()])

DeleteBTN:SetWidth(64);
DeleteBTN:SetHeight(16);
DeleteBTN:SetPoint("LEFT",control,"RIGHT",-8,2);
DeleteBTN:SetScript("OnClick",function()
						   StaticPopupDialogs["DeleteProfile"] = {
						    
						    text = string.gsub(CONFIRM_DELETE[GetLocale()],"$s",UIDropDownMenu_GetSelectedValue(control));
							button1 = TEXT(ACCEPT),
							button2 = TEXT(CANCEL),
							OnAccept = function()
							 crt_deleteProfile(UIDropDownMenu_GetSelectedValue(control));
						     end,
							OnCancel = nil,
							timeout = 0,
							whileDead = 1,
							hideOnEscape = 1,
						   }
						   StaticPopup_Show("DeleteProfile");
                           end);
DeleteBTN:Disable();
SwitchBTN:SetWidth(64);
SwitchBTN:SetHeight(16);
SwitchBTN:SetPoint("LEFT",DeleteBTN,"RIGHT",0,0);
SwitchBTN:Disable();
SwitchBTN:SetScript("OnClick",function() crt_selectProfile(UIDropDownMenu_GetSelectedValue(control)); end);
CreateBTN:SetWidth(64);
CreateBTN:SetHeight(16);
CreateBTN:SetPoint("LEFT",SwitchBTN,"RIGHT",0,0);
CreateBTN:SetScript("OnClick",function()
						   StaticPopupDialogs["NewProfile"] = {
						    text = NAME_PROFILE[GetLocale()];
							button1 = TEXT(ACCEPT),
							button2 = TEXT(CANCEL),
							OnAccept = function()
							 crt_newProfile( getglobal(this:GetParent():GetName().."EditBox"):GetText());
						     end,
							OnCancel = function()
							 getglobal(this:GetParent():GetName().."EditBox"):SetText("");
							 end,
							timeout = 0,
							whileDead = 1,
							hideOnEscape = 1,
							hasEditBox = true
						   }
						   StaticPopup_Show("NewProfile");
                           end);

  UIDropDownMenu_Initialize(control, function() 
   for k,Value in pairs(crt) do
  level = 1;
  local info = UIDropDownMenu_CreateInfo();
    info.text = Value["ProfileName"]; 
    info.value = Value["ProfileName"]; 
	info.owner = getglobal("Profile"); 
	
    info.func = function() UIDropDownMenu_SetSelectedValue(this.owner, this.value); if UIDropDownMenu_GetSelectedValue(control) ~= "DEFAULT" then DeleteBTN:Enable() SwitchBTN:Enable() else DeleteBTN:Disable() end end;
    info.checked = nil; 
    info.icon = nil; 
    UIDropDownMenu_AddButton(info, level);
 end
  end);
UIDropDownMenu_SetSelectedValue(control,crt_activeProfile);
ProfileSelector:SetPoint("TOPLEFT",RegistryWnd,"TOPRIGHT",-36,-36);
ProfileSelector:SetWidth(370);
ProfileSelector:SetHeight(48);
ProfileSelector:Hide();
end
if getglobal("RegistryWndProfileSelector"):IsVisible() then 
getglobal("RegistryWndProfileSelector"):Hide();
else
getglobal("RegistryWndProfileSelector"):Show();
end
end
